package com.example.gss.ServiceImpl;

import com.example.gss.entity.Student;
import com.example.gss.mapper.StudentMapper;
import com.example.gss.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @Transactional
    public Student createStudent(Student student) {
        studentMapper.insert(student);
        return student;
    }

    @Override
    public List<Student> getAllStudents() {
        return studentMapper.selectAll();
    }

    @Override
    public Optional<Student> getStudentById(String studentId) {
        return Optional.ofNullable(studentMapper.selectById(studentId));
    }

    @Override
    @Transactional
    public Student updateStudent(Student student) {
        studentMapper.update(student);
        return student;
    }

    @Override
    @Transactional
    public void deleteStudent(String studentId) {
        studentMapper.delete(studentId);
    }

    @Override
    public List<Student> findStudentsByName(String name) {
        return studentMapper.selectByName(name);
    }

    @Override
    public List<Student> findStudentsByGrade(String grade) {
        return studentMapper.selectByGrade(grade);
    }

    @Override
    public List<Student> findStudentsByTutorId(String tutorId) {
        return studentMapper.selectByTutorId(tutorId);
    }

    @Override
    public List<Student> findActiveStudents() {
        return studentMapper.selectActiveStudents();
    }

    @Override
    public Optional<Student> findByStudentIdAndPassword(String studentId, String password) {
        return Optional.ofNullable(studentMapper.selectByStudentIdAndPassword(studentId, password));
    }
}